package com.demo.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yihujiu.demo.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/3/26.
 */
public class BookBaseAdapter extends RecyclerView.Adapter<BookBaseAdapter.ViewHolder> {

    private ArrayList<String> arr;

    public BookBaseAdapter(ArrayList<String> arr) {
        if (null == arr) {
            this.arr = new ArrayList<String>();
        } else {
            this.arr = arr;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.id.setText(position + "");
        holder.name.setText(arr.get(position));
    }

    @Override
    public int getItemCount() {
        return arr.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView id;
        TextView name;

        public ViewHolder(View view) {
            super(view);
            id = (TextView) view.findViewById(R.id.tv_id);
            name = (TextView) view.findViewById(R.id.tv_name);
        }
    }
}
